package com.chinaV.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinaV.uitls.DBConnection;
import com.chinaV.uitls.Result;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(value = "/updatePassword")
public class UpdatePassword extends HttpServlet {

    private final DBConnection db = new DBConnection();


    public UpdatePassword() {
    }

    public void updadePassword(String username, String password) {
        String sql = "UPDATE user SET password = ? WHERE username = ?";
        PreparedStatement ps = null;
        try {
            ps = this.db.getConn().prepareStatement(sql);
            ps.setString(1, password);
            ps.setString(2, username);
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }

    public boolean userLogin(String username, String password) {
        String sql = "select * from user where username = '" + username + "' and password = '" + password + "'";
//        String sql = "select * from user";
        boolean flag = false;
        try {
            ResultSet resultSet = this.db.getSt().executeQuery(sql);
            flag = resultSet.next();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return false;
        }

        return flag;
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        HttpServletResponse response =  (HttpServletResponse) resp;
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();

        // 获取表单数据
        BufferedReader reader = req.getReader();
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
        }

        String jsonString = stringBuilder.toString();

        JSONObject json = JSON.parseObject(jsonString);

        String username = json.getString("username");
        String oldPassword = json.getString("oldPassword");
        String newPassword = json.getString("newPassword");
        String confirmPassword = json.getString("confirmPassword");


        if (!newPassword.equals(confirmPassword)) {

            out.print(JSON.toJSONString(Result.error("error!")));
            return;
        }
        if (userLogin(username, oldPassword)) {
            updadePassword(username, newPassword);
            out.print(JSON.toJSONString(Result.success("Yes!")));
//            return;
            resp.sendRedirect("/login.jsp");
        } else {
            resp.sendRedirect("/fail.html");
        }
    }


}

